package com.dnw.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.dnw.modle.DummyContent.DummyItem;
import com.dnw.modle.Funnys;
import com.dnw.shyfunny.R;
import com.dnw.util.GlideUtils;
import com.dnw.view.CircleProgressView;

import java.math.BigDecimal;
import java.util.List;


/**
 * {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
 * TODO: Replace the implementation with code for your data type.
 */
public class LocalShowAdapter extends RecyclerView.Adapter<LocalShowAdapter.ViewHolder> {

    private final List<String> mValues;
    private Context mContext;

    public LocalShowAdapter(Context context, List<String> items) {
        mValues = items;
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_image_show, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        GlideUtils.load(mContext, mValues.get(position), holder.mIdView);
        holder.mIdView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupWindowRegist(mValues.get(position));
            }
        });
    }


    private RelativeLayout layout;
    private PopupWindow popupWindow;
    public void showPopupWindowRegist(String urlImage) {
        int typeCount = urlImage.indexOf(".");
        String urlType = urlImage.substring(typeCount+1, urlImage.length());
        String type = urlType.equalsIgnoreCase("gif") ? "GIF" : "IMAGE";
        // 加载布局
        if("GIF".equalsIgnoreCase(type)){
            layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(
                    R.layout.dialog_show_gif, null);
            popupWindow = new PopupWindow(layout, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
            ImageView iv_img = (ImageView) (layout).findViewById(R.id.iv_img);
            CircleProgressView mCircleProgressView = (CircleProgressView) (layout).findViewById(R.id.iv_play);
            mCircleProgressView.setVisibility(View.GONE);
            GlideUtils.load(mContext, urlImage, iv_img);
            iv_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });
        } else  if("IMAGE".equalsIgnoreCase(type)){
            layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(
                    R.layout.dialog_regist, null);
            popupWindow = new PopupWindow(layout, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
//            ImageView iv_show = (ImageView) (layout).findViewById(R.id.iv_show);
//            GlideUtils.load(mContext, funnys.bigImage, iv_show);
            SubsamplingScaleImageView iv_show = (SubsamplingScaleImageView) (layout).findViewById(R.id.iv_show);

            iv_show.setMinScale(1.0F);//最小显示比例
            iv_show.setMaxScale( 10.0f);//最大显示比例
            //下载图片保存到本地
//            Glide.with(mContext)
//                    .load(funnys.bigImage)
//                    .downloadOnly(new SimpleTarget<File>() {
//                @Override
//                public void onResourceReady(File resource, Transition<? super File> transition) {
//                    iv_show.setImage(ImageSource.uri(Uri.fromFile(resource)), new ImageViewState(1.0F, new PointF(0, 0), 0));
//                }
//                    });

            Glide.with(mContext).asBitmap().load(urlImage).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                    BigDecimal widthB = new BigDecimal(resource.getWidth());
                    BigDecimal heightB = new BigDecimal(resource.getHeight());
                    if(widthB.divide(heightB, 2,BigDecimal.ROUND_DOWN).compareTo(new BigDecimal(0.5)) > 0){
                        iv_show.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE);
                    }else{
                        iv_show.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
                    }
                    iv_show.setImage(ImageSource.bitmap(resource), new ImageViewState(1.0F, new PointF(0, 0), 0));
                }
            });
            iv_show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });
        }
        TextView tv_pop_save = (TextView) (layout).findViewById(R.id.tv_pop_save);
        TextView tv_pop_share = (TextView) (layout).findViewById(R.id.tv_pop_share);
        LinearLayout ll_share_save = (LinearLayout) (layout).findViewById(R.id.ll_share_save);
        ll_share_save.setVisibility(View.GONE);
        // 找到布局的控件
        // 实例化popupWindow

        // 控制键盘是否可以获得焦点
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
//		popupWindow.setBackgroundDrawable(new PaintDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
//		// 设置popupWindow弹出窗体的背景
//		WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        // 监听

        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        tv_pop_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        tv_pop_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                ShareUtil.showShare(mContext, funnys);
//                popupWindow.dismiss();
            }
        });
        popupWindow.showAsDropDown(tv_pop_save);
    }


    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final ImageView mIdView;
        public String mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (ImageView) view.findViewById(R.id.iv_img);
        }

    }
}
